#
# Needs work! As Sam if documentation needed.
#



import csv
import xml.dom.minidom
import sys





def createModel(kmlDoc, row, order):
    #
    # This creates a 3D model of a turbine
    #
    placemarkElement = kmlDoc.createElement('Placemark')

    modElement = kmlDoc.createElement('Model')
    modElement.setAttribute('name', row['Turbine'])
    placemarkElement.appendChild(modElement)
    nameElement = kmlDoc.createTextNode(coordinates)
    extElement = kmlDoc.createElement('ExtendedData')

    coorElement.appendChild(kmlDoc.createTextNode(coordinates))


    # Loop through the columns and create a  element for every field that has a value.
    for key in order:
        if row[key]:
            dataElement = kmlDoc.createElement('Data')
            dataElement.setAttribute('name', key)
            valueElement = kmlDoc.createElement('value')
            dataElement.appendChild(valueElement)
            valueText = kmlDoc.createTextNode(row[key])
            valueElement.appendChild(valueText)
            extElement.appendChild(dataElement)

    coordinates = '%s,%s' %(row['Longitude'],row['Latitude'])
    print coordinates
    pointElement = kmlDoc.createElement('Point')
    placemarkElement.appendChild(pointElement)

    coorElement = kmlDoc.createElement('coordinates')
    coorElement.appendChild(kmlDoc.createTextNode(coordinates))
    pointElement.appendChild(coorElement)
    return placemarkElement



def createPlacemark(kmlDoc, row, order):
    # This creates a  element for a row of data.
    # A row is a dict.
    placemarkElement = kmlDoc.createElement('Placemark')
    extElement = kmlDoc.createElement('ExtendedData')
    placemarkElement.appendChild(extElement)

    # Loop through the columns and create a  element for every field that has a value.
    for key in order:
        if row[key]:
            dataElement = kmlDoc.createElement('Data')
            dataElement.setAttribute('name', key)
            valueElement = kmlDoc.createElement('value')
            dataElement.appendChild(valueElement)
            valueText = kmlDoc.createTextNode(row[key])
            valueElement.appendChild(valueText)
            extElement.appendChild(dataElement)

    coordinates = '%s,%s' %(row['Longitude'],row['Latitude'])
    print coordinates
    pointElement = kmlDoc.createElement('Point')
    placemarkElement.appendChild(pointElement)

    coorElement = kmlDoc.createElement('coordinates')
    coorElement.appendChild(kmlDoc.createTextNode(coordinates))
    pointElement.appendChild(coorElement)
    return placemarkElement

def createKML(csvReader, fileName, order):
    # This constructs the KML document from the CSV file.
    kmlDoc = xml.dom.minidom.Document()

    kmlElement = kmlDoc.createElementNS('http://earth.google.com/kml/2.2', 'kml')
    kmlElement.setAttribute('xmlns','http://earth.google.com/kml/2.2')
    kmlElement = kmlDoc.appendChild(kmlElement)
    documentElement = kmlDoc.createElement('Document')
    documentElement = kmlElement.appendChild(documentElement)

    # Skip the header line.
    csvReader.next()

    for row in csvReader:
        placemarkElement = createPlacemark(kmlDoc, row, order)
        documentElement.appendChild(placemarkElement)

    kmlFile = open(fileName, 'w')
    kmlFile.write(kmlDoc.toprettyxml('  ', newl = '\n', encoding = 'utf-8'))

def main():

  # If an argument was passed to the script, it splits the argument on a comma
  # and uses the resulting list to specify an order for when columns get added.
  # Otherwise, it defaults to the order used in the sample.

    if len(sys.argv) >1:
        order = sys.argv[1].split(',')
    else:
        order = ['Turbine','Park','Country','Manufacture','Turbine Type','Hub Height',
                 'Park Type','Latitude','Longitude']

    csvreader = csv.DictReader(open(r'H:\Documents\work\domains\turbines\turbine_locations.csv'),order)
    kml       = createKML(csvreader, r'H:\Documents\work\domains\turbines\turbine_locations.kml', order)

if __name__ == '__main__':
    main()